package com.wangxiaoxuan.seckill.service.ex;

import com.wangxiaoxuan.seckill.vo.ResBeanEnum;
import com.wangxiaoxuan.seckill.vo.RespBean;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.net.BindException;

/**
 * @author 王小轩
 * @version 1.0
 * 该类处理程序运行过程中的异常
 */
@RestControllerAdvice
public class BaseExceptionHandler {
    @ExceptionHandler(Exception.class)
    public RespBean ExceptionHandler(Exception e){
        if (e instanceof BaseException){
            BaseException ex = (BaseException) e;
            return RespBean.error(ex.getResBeanEnum());
        }else if (e instanceof BindException){
            BaseException ex = (BaseException) e;
            RespBean respBean = RespBean.error(ResBeanEnum.BIND_ERROR);
            return respBean;
        }
        return RespBean.error(ResBeanEnum.ERROR);
    }
}
